<template>
  <div class='container'>
    <el-dialog
        :title="title"
        @open="getdetail()"
        :visible.sync="dialogVisible"
        width="30%">
        <el-form label-width="80px" ref="from" :model="subAddForm" :rules="rules">
            <el-form-item label="学科名称" prop="subjectName">
              <el-input v-model="subAddForm.subjectName" placeholder="请输入学科名称"></el-input>
            </el-form-item>
            <el-form-item label="学科名称">
              <el-switch
                v-model="subAddForm.isFrontDisplay"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closedialog">取 消</el-button>
          <el-button type="primary" @click="addSub">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/subjects.js'
export default {
  name: 'SubjectsAdd',
  props: {
    dataLIst: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      title: '新增学科',
      dialogVisible: false,
      subAddForm: {
        subjectName: '', // 学科名称
        isFrontDisplay: 1// 是否在页面显示 1是 0否
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    isShowdialog () {
      this.dialogVisible = true
    },
    // 关闭弹窗
    closedialog () {
      this.dialogVisible = false
      this.$refs.from.resetFields()
      this.subAddForm = {
        subjectName: '', // 学科名称
        isFrontDisplay: 1// 是否在页面显示 1是 0否
      }
    },
    // 点击确定按钮
    addSub () {
      this.$refs.from.validate(async boo => {
        if (!boo) return null
        if (this.subAddForm.id) {
          const res = await update(this.subAddForm)
          if (res.status !== 200) return this.$message.error('修改学科失败')
          this.$message.success('修改学科成功')
        } else {
          const res = await add(this.subAddForm)
          if (res.status !== 200) return this.$message.error('修改学科失败')
          this.$message.success('新增学科成功')
        }
        this.closedialog()
        this.$emit('getSubjectList')
      })
    },
    async getdetail () {
      if (this.dataLIst.id) {
        const { data } = await detail(this.dataLIst)
        this.subAddForm = data
      }
    }

  }
}
</script>

<style scoped lang='less'></style>
